// mddagp.h
// multi dimensional dynamic array generic programming

template <typename T>
T **allocMatrixMddagp(int r, int c)
{
	T **ret;
	ret = new T* [r];
	for (int i=0 ; i < r ; i++)
		ret[i] = new T [c];
	return ret;
};


template <typename T>
void fillMatrixMddagp(T **p, int r, int c, T d)
{
	for (int i=0 ; i < r ; i++) {
		for (int j=0 ; j < c ; j++) {
			p[i][j] = d;
		}
	}
};

template <typename T>
void addToMatrixMddagp(T **p, int r, int c, T d)
{
	for (int i=0 ; i < r ; i++) {
		for (int j=0 ; j < c ; j++) {
			p[i][j] += d;
		}
	}
};

template <typename T>
void deleteMatrixMddagp(T **p, int r)
{
	for (int i=0 ; i < r ; i++)
		delete [] p[i];
	delete [] p;
};

template <typename T>
void multidimensional_dynamic_array_generic_programming(int size)
{
	cout << "=== multidimensional_dynamic_array_generic_programming, size=" << size << endl;
	T eps = 1e-8;
	MyClock clk;
	double duration_sec;
	clk.markTime();
	// code to test begin (1)
	int row = size, col = size;
	T **p = allocMatrixMddagp<T>(row,col);
	fillMatrixMddagp<T>(p,row,col,1.1);
	duration_sec = clk.getDiffTime();
	cout << "alloc & fill matrix=" << duration_sec << " sec" << endl;
	// code to test begin (2)
	clk.markTime();
	addToMatrixMddagp<T>(p,row,col,2.2);
	duration_sec = clk.getDiffTime();
	cout << "addToMatrix=" << duration_sec << " sec" << endl;
	// code to test end (2)

	// confirm that add was done
	myAssert(nearEqual(p[2][3],3.3,eps));

	// code to test begin (3)
	clk.markTime();
	deleteMatrixMdda(p,row);
	duration_sec = clk.getDiffTime();
	cout << "deleteMatrix=" << duration_sec << " sec" << endl;
	// code to test end (3)
}
